using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FiveOpen
{
    public int[,] map;
    public int currentPlayer;

    private int mapWidth;
    private int mapHeight;
    

    public FiveOpen()
    {
        ResetChess();
    }

    public FiveOpen Clone()
    {
        FiveOpen fo = new FiveOpen();
        for (int i = 0; i < mapWidth; i++)
        {
            for (int j = 0; j < mapHeight; j++)
            {
                fo.map[i, j] = map[i, j];
            }
        }

        fo.currentPlayer = currentPlayer;

        return fo;
    }


    public bool CheckCanPlace(int x, int y)
    {
        if (map[x, y] != 0)
        {
            return false;
        }

        return true;
    }

    public void PlacePiece(int x, int y)
    {
        map[x, y] = currentPlayer;
    }

    public void TurnPlayer()
    {
        currentPlayer = -currentPlayer;
    }

    public void ResetChess()
    {
        map = new int[8, 8];
        // map[0, 0] = 1;
        // map[0, 1] = 1;
        // map[0, 2] = 1;
        // map[0, 3] = 1;
        // map[0, 4] = 1;
        // map[1, 1] = -1;
        // map[1, 2] = -1;
        // map[1, 3] = -1;
        // map[1, 4] = -1;
        currentPlayer = 1;

        mapWidth = map.GetLength(0);
        mapHeight = map.GetLength(1);
    }

    public ChessResult CheckResult()
    {
        for (int x = 0; x < mapWidth - 4; x++)
        {
            for (int y = 0; y < mapHeight; y++)
            {
                var curPiece = map[x, y];
                if (curPiece == 0)
                {
                    continue;
                }

                bool win = true;
                for (int i = 1; i < 5; i++)
                {
                    if (map[x + i, y] != curPiece)
                    {
                        win = false;
                        break;
                    }
                }

                if (win)
                {
                    return curPiece == 1 ? ChessResult.BlackWin : ChessResult.WhiteWin;
                }
            }
        }

        for (int x = 0; x < mapWidth; x++)
        {
            for (int y = 0; y < mapHeight - 4; y++)
            {
                var curPiece = map[x, y];
                if (curPiece == 0)
                {
                    continue;
                }

                bool win = true;
                for (int i = 1; i < 5; i++)
                {
                    if (map[x, y + i] != curPiece)
                    {
                        win = false;
                        break;
                    }
                }

                if (win)
                {
                    return curPiece == 1 ? ChessResult.BlackWin : ChessResult.WhiteWin;
                }
            }
        }

        for (int x = 0; x < mapWidth - 4; x++)
        {
            for (int y = 0; y < mapHeight - 4; y++)
            {
                var curPiece = map[x, y];
                if (curPiece == 0)
                {
                    continue;
                }

                bool win = true;
                for (int i = 1; i < 5; i++)
                {
                    if (map[x + i, y + i] != curPiece)
                    {
                        win = false;
                        break;
                    }
                }

                if (win)
                {
                    return curPiece == 1 ? ChessResult.BlackWin : ChessResult.WhiteWin;
                }
            }
        }

        for (int x = 4; x < mapWidth; x++)
        {
            for (int y = 0; y < mapHeight - 4; y++)
            {
                var curPiece = map[x, y];
                if (curPiece == 0)
                {
                    continue;
                }

                bool win = true;
                for (int i = 1; i < 5; i++)
                {
                    if (map[x - i, y + i] != curPiece)
                    {
                        win = false;
                        break;
                    }
                }

                if (win)
                {
                    return curPiece == 1 ? ChessResult.BlackWin : ChessResult.WhiteWin;
                }
            }
        }

        bool draw = true;
        for (int x = 0; x < mapWidth; x++)
        {
            for (int y = 0; y < mapHeight; y++)
            {
                if (map[x, y] == 0)
                {
                    draw = false;
                    break;
                }
            }
        }

        return draw ? ChessResult.Draw : ChessResult.Incomplete;
    }
}

public enum ChessResult
{
    Incomplete = 2,
    BlackWin = 1,
    WhiteWin = -1,
    Draw = 0
}